﻿using HR.Config;
using HR.Filters;
using HR.IService;
using HR.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace HR.Controllers
{
    public class ConfigFileController : Controller
    {
        private readonly ILogger<ConfigFileController> logger;
        private readonly IFirst_kindService fkService;
        private readonly ISecond_kindService skService;
        private readonly IThird_kindService tsService;

        public ConfigFileController(ILogger<ConfigFileController> logger,IFirst_kindService fkService,ISecond_kindService skService, IThird_kindService tsService)
        {
            this.logger = logger;
            this.fkService = fkService;
            this.skService = skService;
            this.tsService = tsService;
        }
        #region I级操作
        /// <summary>
        /// I级机构设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult OneInstit()
        {
            return View();
        }
        /// <summary>
        /// 查三级机构一并丢出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> fstKindsFill()
        {
            var list = new List<object>();
            Paging<config_file_first_kind> p1 = await fkService.FirstKindPage();
            Paging<config_file_second_kind> p2 = await skService.SecondKindPage();
            Paging<config_file_third_kind> p3 = await tsService.ThirdKindPage();
            list.Add(p1);
            list.Add(p2);
            list.Add(p3);
            return Json(list);
        }
        /// <summary>
        /// I级机构设置刷新
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> OneInstitFill()
        {
            Paging<config_file_first_kind> pgtab = await fkService.FirstKindPage();
            return Json(pgtab);
        }
        /// <summary>
        /// 新增一级
        /// </summary>
        /// <param name="ffk"></param>
        [HttpPost]
        public async Task<IActionResult> AddFirstKind([FromBody] config_file_first_kind ffk)
        {
            int res = await fkService.AddFirstKind(ffk);
            return Json(res);
        }
        /// <summary>
        /// 修改一级
        /// </summary>
        /// <param name="ffk"></param>
        [HttpPost]
        public async Task<IActionResult> UpdFirstKind([FromBody] config_file_first_kind ffk)
        {
            int res = await fkService.UdpFirstKind(ffk);
            return Json(res);
        }
        /// <summary>
        /// 删除一级
        /// </summary>
        /// <param name="ffk"></param>
        [HttpDelete]
        public async Task<IActionResult> DelFirstKind(short ffk_id)
        {
            int res = await fkService.DelFirstKind(ffk_id);
            return Json(res);
        }
        #endregion

        #region II级操作
        /// <summary>
        /// II级机构设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult TwoInstit()
        {
            return View();
        }
        /// <summary>
        /// II级机构设置刷新
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> TwoInstitFill()
        {
            Paging<config_file_second_kind> pgtab = await skService.SecondKindPage();
            return Json(pgtab);
        }
        /// <summary>
        /// 新增二级
        /// </summary>
        /// <param name="fsk"></param>
        [HttpPost]
        public async Task<IActionResult> AddSecondKind([FromBody] config_file_second_kind fsk)
        {
            int res = await skService.AddSecondKind(fsk);
            return Json(res);
        }
        /// <summary>
        /// 修改二级
        /// </summary>
        /// <param name="fsk"></param>
        [HttpPost]
        public async Task<IActionResult> UpdSecondKind([FromBody] config_file_second_kind fsk)
        {
            int res = await skService.UdpSecondKind(fsk);
            return Json(res);
        }
        /// <summary>
        /// 删除二级
        /// </summary>
        /// <param name="fsk_id"></param>
        [HttpDelete]
        public async Task<IActionResult> DelSecondKind(short fsk_id)
        {
            int res = await skService.DelSecondKind(fsk_id);
            return Json(res);
        }
        #endregion

        #region III级操作
        /// <summary>
        /// III级机构视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult ThreeInstit()
        {
            return View();
        }
        /// <summary>
        /// III级机构设置刷新
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ThreeInstitFill()
        {
            Paging<config_file_third_kind> pgtab = await tsService.ThirdKindPage();
            return Json(pgtab);
        }

        /// <summary>
        /// 联级选择器
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> CascaderSele()
        {
            List<CFFK> cffks = await tsService.CascaderSele();
            return Json(cffks);
        }

        /// <summary>
        /// 新增三级
        /// </summary>
        /// <param name="ftk"></param>
        [HttpPost]
        public async Task<IActionResult> AddThirdKind([FromBody]config_file_third_kind ftk)
        {
            int res = await tsService.AddThirdKind(ftk);
            return Json(res);
        }
        /// <summary>
        /// 修改三级
        /// </summary>
        /// <param name="ftk"></param>
        [HttpPost]
        public async Task<IActionResult> UdpThirdKind([FromBody]config_file_third_kind ftk)
        {
            int res = await tsService.UdpThirdKind(ftk);
            return Json(res);
        }
        /// <summary>
        /// 删除三级
        /// </summary>
        /// <param name="ftkid"></param>
        [HttpDelete]
        public async Task<IActionResult> DelThirdKind(short ftk_id)
        {
            int res = await tsService.DelThirdKind(ftk_id);
            return Json(res);
        }
        #endregion

        /// <summary>
        /// 一次性查询三级机构,用于联级选择器
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ConfigFilesFind()
        {
            List<CFFK> list = await tsService.ConfigFilesFind();
            return Json(list);
        }
    }
}
